package jmine.tec.batch.services;

import jmine.tec.batch.domain.MPhase;
import jmine.tec.batch.domain.MProcess;
import jmine.tec.batch.export.MPhaseToMProcessAssociation;
import jmine.tec.services.annotations.Execution;
import jmine.tec.services.annotations.Input;
import jmine.tec.services.annotations.ServiceImplementor;
import jmine.tec.services.api.ActionsEnum;
import jmine.tec.services.api.export.ReferenceMap;
import jmine.tec.services.api.export.ServiceFiller;
import jmine.tec.services.io.ServiceBean;

/**
 * Serviço que adiciona uma {@link MPhase} em um {@link MProcess}
 * 
 * @author Rafael Volpato
 * @date Oct 30, 2009
 */
@ServiceImplementor(action = ActionsEnum.INCLUIR)
public class MPhaseToMProcessService implements ServiceFiller<MPhaseToMProcessAssociation> {

    private static final String MPHASE = "Fase";

    private static final String MPROCESS = "Processo";

    private static final String SERVICE_NAME = "MPhaseToMProcess";

    private MPhase phase;

    private MProcess process;

    /**
     * @param phase the phase to set
     */
    @Input(fieldName = MPHASE)
    public void setPhase(MPhase phase) {
        this.phase = phase;
    }

    /**
     * @param process the process to set
     */
    @Input(fieldName = MPROCESS)
    public void setProcess(MProcess process) {
        this.process = process;
    }

    /**
     * Adiciona a {@link MPhase} ao {@link MProcess}.
     */
    @Execution
    public void execute() {
        this.process.addPhase(this.phase);
        this.process.getPersister().save();
    }

    /**
     * {@inheritDoc}
     */
    public void fillServiceBean(ServiceBean bean, ReferenceMap referenceMap, MPhaseToMProcessAssociation entity) {
        bean.setAction(ActionsEnum.INCLUIR);
        bean.setName(SERVICE_NAME);

        bean.putParameter(MPHASE, entity.getBaseEntity().getMnemonico());
        bean.putParameter(MPROCESS, entity.getAssociatedEntity().getMnemonico());
    }
}
